#ifndef TRIANGS_H_
#define TRIANGS_H_

#include <SFML/Graphics.hpp>

#include "igame.hpp"
#include "../glm/glm.hpp"

class Triangs: public lge2d::IGame {
private:
	std::vector<glm::vec2> points;
	const size_t max_points;
	const int poly_vertex_count;
	glm::vec2 triangle[3];
	glm::vec2 origin_point;
public:
	Triangs() :
			max_points(10000), poly_vertex_count(
					sizeof(triangle) / sizeof(glm::vec2)) {
	}

	virtual void Initialize() {
		points.reserve(max_points);

		triangle[0] = glm::vec2(0.f, 0.f);
		triangle[1] = glm::vec2(1.0f, 0.f);
		triangle[2] = glm::vec2(1.f, 1.0f);

		for (int i = 0; i < poly_vertex_count; ++i) {
			points.push_back(triangle[i]);
		}

		origin_point = glm::vec2(0.5f, 0.25f);
	}
	virtual void Update(float deltaTime) {
		// update. generate next point
		if (points.capacity() > points.size()) {
			int triangle_index = rand() % poly_vertex_count;
			const glm::vec2 & random_vert = triangle[triangle_index];
			origin_point = (origin_point + random_vert) / 2.f;
			points.push_back(origin_point);
		}
	}
	virtual void OnEvent(const sf::Event& event) {
		// empty for now
		(void)0;
	}
	virtual void Render() const {
		// render
		glClear(GL_COLOR_BUFFER_BIT);
		glEnableClientState(GL_VERTEX_ARRAY);
		const size_t vec_component_count = sizeof(glm::vec2) / sizeof(GLfloat);
		glVertexPointer(vec_component_count, GL_FLOAT, 0, &points[0]);
		glDrawArrays(GL_POINTS, 0, points.size());
	}

	virtual ~Triangs() {
	}
};

#endif /* TRIANGS_H_ */
